import { CoolCommException } from '@cool-midway/core';
import { Rule, RuleType } from '@midwayjs/validate';
import { DemandHallPostType, DemandHallStatus, VisibilityRules } from '../entity/hall';

export class PublishDemandHallDTO {
  @Rule(
    RuleType.number()
      .valid(DemandHallPostType.SUPPLY, DemandHallPostType.DEMAND)
      .default(DemandHallPostType.SUPPLY)
      .error(new CoolCommException('发布类型不合法'))
  )
  postType: DemandHallPostType;

  @Rule(
    RuleType.number()
      .min(0)
      .max(3)
      .default(0)
      .error(new CoolCommException('资源类型不合法'))
  )
  resourceType: number;

  @Rule(
    RuleType.string()
      .required()
      .min(4)
      .max(100)
      .error(new CoolCommException('标题需在4-100个字符之间'))
  )
  title: string;

  @Rule(
    RuleType.string()
      .required()
      .min(10)
      .max(2000)
      .error(new CoolCommException('描述需在10-2000个字符之间'))
  )
  description: string;

  @Rule(
    RuleType.string()
      .allow('')
      .max(100)
      .error(new CoolCommException('所在地不能超过100个字符'))
  )
  location?: string;

  @Rule(
    RuleType.string()
      .allow('')
      .max(100)
      .error(new CoolCommException('可用时间不能超过100个字符'))
  )
  availableTime?: string;

  @Rule(
    RuleType.number()
      .optional()
      .min(0)
      .error(new CoolCommException('预算/报价需大于0'))
  )
  budgetAmount?: number;

  @Rule(
    RuleType.string()
      .required()
      .max(50)
      .error(new CoolCommException('联系人姓名不能为空且最长50个字符'))
  )
  contactName: string;

  @Rule(
    RuleType.string()
      .required()
      .max(100)
      .error(new CoolCommException('联系方式不能为空且最长100个字符'))
  )
  contactInfo: string;

  @Rule(
    RuleType.string()
      .allow('')
      .max(500)
      .error(new CoolCommException('备注不能超过500个字符'))
  )
  remark?: string;

  @Rule(
    RuleType.number()
      .integer()
      .min(1)
      .max(30)
      .default(7)
      .error(new CoolCommException('有效期天数需在1-30天之间'))
  )
  expireDays: number;

  @Rule(RuleType.object().optional())
  visibilityRules?: VisibilityRules;
}

export class UpdateDemandHallDTO {
  @Rule(
    RuleType.number()
      .required()
      .error(new CoolCommException('发布ID不能为空'))
  )
  id: number;

  @Rule(
    RuleType.number()
      .valid(DemandHallPostType.SUPPLY, DemandHallPostType.DEMAND)
      .optional()
      .error(new CoolCommException('发布类型不合法'))
  )
  postType?: DemandHallPostType;

  @Rule(
    RuleType.number()
      .min(0)
      .max(3)
      .optional()
      .error(new CoolCommException('资源类型不合法'))
  )
  resourceType?: number;

  @Rule(
    RuleType.string()
      .optional()
      .min(4)
      .max(100)
      .error(new CoolCommException('标题需在4-100个字符之间'))
  )
  title?: string;

  @Rule(
    RuleType.string()
      .optional()
      .min(10)
      .max(2000)
      .error(new CoolCommException('描述需在10-2000个字符之间'))
  )
  description?: string;

  @Rule(
    RuleType.string()
      .allow('')
      .max(100)
      .optional()
      .error(new CoolCommException('所在地不能超过100个字符'))
  )
  location?: string;

  @Rule(
    RuleType.string()
      .allow('')
      .max(100)
      .optional()
      .error(new CoolCommException('可用时间不能超过100个字符'))
  )
  availableTime?: string;

  @Rule(
    RuleType.number()
      .optional()
      .min(0)
      .error(new CoolCommException('预算/报价需大于0'))
  )
  budgetAmount?: number;

  @Rule(
    RuleType.string()
      .optional()
      .max(50)
      .error(new CoolCommException('联系人姓名不能为空且最长50个字符'))
  )
  contactName?: string;

  @Rule(
    RuleType.string()
      .optional()
      .max(100)
      .error(new CoolCommException('联系方式不能为空且最长100个字符'))
  )
  contactInfo?: string;

  @Rule(
    RuleType.string()
      .allow('')
      .max(500)
      .optional()
      .error(new CoolCommException('备注不能超过500个字符'))
  )
  remark?: string;

  @Rule(
    RuleType.number()
      .integer()
      .min(1)
      .max(30)
      .optional()
      .error(new CoolCommException('有效期天数需在1-30天之间'))
  )
  expireDays?: number;

  @Rule(RuleType.object().optional())
  visibilityRules?: VisibilityRules;
}

export class UpdateDemandHallStatusDTO {
  @Rule(
    RuleType.number()
      .required()
      .error(new CoolCommException('发布ID不能为空'))
  )
  id: number;

  @Rule(
    RuleType.number()
      .valid(
        DemandHallStatus.OPEN,
        DemandHallStatus.NEGOTIATING,
        DemandHallStatus.CLOSED,
        DemandHallStatus.EXPIRED
      )
      .error(new CoolCommException('状态不合法'))
  )
  status: DemandHallStatus;

  @Rule(
    RuleType.string()
      .allow('')
      .max(500)
      .error(new CoolCommException('备注不能超过500个字符'))
  )
  remark?: string;
}

export class DemandHallPageDTO {
  @Rule(RuleType.number().optional())
  postType?: DemandHallPostType;

  @Rule(RuleType.number().optional())
  resourceType?: number;

  @Rule(
    RuleType.alternatives()
      .try(
        RuleType.number(),
        RuleType.array().items(RuleType.number())
      )
      .optional()
  )
  status?: DemandHallStatus | DemandHallStatus[];

  @Rule(RuleType.string().optional())
  location?: string;

  @Rule(RuleType.string().optional())
  keyword?: string;

  @Rule(RuleType.number().default(1))
  page?: number;

  @Rule(RuleType.number().default(20))
  size?: number;
}
